# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit eutils flag-o-matic toolchain-funcs

IUSE="nls"

#need to check what gcc version we are running
GCC_PV=`gcc-version`
GCC_PVS=`gcc-major-version`.`gcc-minor-version`.`gcc-micro-version`
GCC_PVF=`gcc-fullversion`
GCC_LIBS="`gcc-config --get-lib-path`"

DESCRIPTION="D GCC Frontend"
SRC_URI="mirror://sourceforge/dgcc/gdc-${PV}-src.tar.bz2
		ftp://gcc.gnu.org/pub/gcc/releases/gcc-${GCC_PVS}/gcc-${GCC_PVS}.tar.bz2"

HOMEPAGE="http://dgcc.sourceforge.net/"

SLOT="0"
KEYWORDS="~x86 ~amd64"
LICENSE="GPL-2"
S="${WORKDIR}/gcc-${GCC_PVS}"

DEPEND="virtual/libc
		>=sys-devel/gcc-3.3.6"

RDEPEND="virtual/libc
		>=sys-devel/gcc-3.3.6"

src_unpack() {
	unpack "gcc-${GCC_PVS}.tar.bz2"

	cd "${S}/gcc"

	unpack "gdc-${PV}-src.tar.bz2"

	cd "${S}"

	if [ "${GCC_PV}" != "3.3" -a "${GCC_PV}" != "3.4" -a "${GCC_PV}" != "4.0" ]
	then
	    die "GCC ${GCC_PV} not supported by GDC"
	fi
	gcc/d/setup-gcc.sh || die

	# apply branding. must be done here since setup-gcc.sh also changes the version.
	# adapted from gcc_version_patch() (found in toolchain.eclass)
	local release_version="Gentoo ${PVR}"
	sed -i -e "s~\(const char version_string\[\] = \".*\)\()\".*\)~\1, ${release_version}\2~" \
	       -e 's~http:\/\/gcc\.gnu\.org\/bugs\.html~http:\/\/bugs\.gentoo\.org\/~' "${S}/gcc/version.c" || die "failed to update version.c with Gentoo branding."
}

src_compile() {
	einfo "Configuring GCC for GDC inclusion ..."

	strip-flags

	# In general gcc does not like optimization, and add -O2 where
	# it is safe.  This is especially true for gcc 3.3 + 3.4
	replace-flags -O? -O2

	# -mcpu is deprecated on these archs, and possibly others
	if use amd64 || use x86 ; then
		setting="`get-flag mcpu`"
		[ ! -z "${setting}" ] && \
			replace-flags -mcpu="${setting}" -mtune="${setting}" && \
			ewarn "-mcpu is deprecated on your arch\a\a\a" && \
			epause 5
	fi

	strip-unsupported-flags

	mkdir -p "${WORKDIR}/gdc-build"
	cd "${WORKDIR}/gdc-build"
	# TODO: --enable-languages=d is sufficient when no 'recls' support is needed.
	ECONF_SOURCE="${S}"
	econf --enable-shared --enable-threads=posix --enable-languages=c++,d

	einfo "Building GDC and Phobos ..."
	gmake
}

src_install() {
	cd "${WORKDIR}"

	dobin gdc-build/gcc/gdc
	doman gcc-${GCC_PVS}/gcc/d/gdc.1

	# rename to gdmd but move to ${WORKDIR} since there already is a directory named "dmd" in old location
	mv "gcc-${GCC_PVS}/gcc/d/dmd-script" "${WORKDIR}/gdmd"
	mv "gcc-${GCC_PVS}/gcc/d/dmd-script.1" "${WORKDIR}/gdmd.1"
	dobin "${WORKDIR}/gdmd"
	doman "${WORKDIR}/gdmd.1"
	mv "${WORKDIR}/gdmd" "gcc-${GCC_PVS}/gcc/d/dmd-script"
	mv "${WORKDIR}/gdmd.1" "gcc-${GCC_PVS}/gcc/d/dmd-script.1"

	dodir "${GCC_LIBS}"
	cp gdc-build/gcc/cc1d ${D}"${GCC_LIBS}"

	cd "${WORKDIR}/gdc-build/*/libphobos"
	make DESTDIR="${D}" install
}
